import { transform } from '@/transform'

describe('transform', () => {

  const source = {
    name: 'John',
    age: 30,
    hobby1: 'reading',
    hobby2: 'running',
    hobby3: 'swimming',
    couple: null,
  }

  test('应该能提取属性', () => {
    const result = transform(source, ['name', 'age'])
    expect(result).toEqual({
      name: 'John',
      age: 30,
    })
  })

  test('应该能提取属性并改名', () => {
    const result = transform(source, [{name: 'title'}])
    expect(result).toEqual({
      title: 'John',
    })
  })

  test('应该能提取属性并改值', () => {
    const result = transform(source, [{age: (val: number) => val + 1}])
    expect(result).toEqual({
      age: 31,
    })
  })

  test('应该能提取属性并改名和值', () => {
    const result = transform(source, [{name: {alias: 'title', convert: (val) => val + 'x'}}])
    expect(result).toEqual({
      title: 'Johnx',
    })
  })

  test('当allowNull为false时，应该能忽略值为null或undefined的属性', () => {
    const result = transform(source, ['couple', 'unknown'], {allowNull: false})
    expect(result).toEqual({})
  })

  test('应该能进行属性合并和拆分', () => {
    const result = transform(source, [{hobby: {alias: 'hobbyList', convert: (_, obj: any) => [obj.hobby1, obj.hobby2, obj.hobby3]}}]) as any
    expect(result).toEqual({
      hobbyList: ['reading', 'running', 'swimming'],
    })

    const result2 = transform(result, [
      {hobbyList: {alias: 'hobby1', convert: (v) => v, globals: (v) => { const list = v as string[]; return {hobby1: list[0], hobby2: list[1], hobby3: list[2]}}}}
    ])
    expect(result2).toEqual({
      hobby1: 'reading',
      hobby2: 'running',
      hobby3: 'swimming',
    })
  })

})